1 using System.Collections.Generic;
2 using UnityEngine;
3
4 namespace ProceduralToolkit.Examples
5 {
6 public class ConfiguratorBase : MonoBehaviour
7 {
8 private Palette currentPalette = new Palette();
9 private Palette targetPalette = new Palette();
10
11 protected static T InstantiateControl<T>(Transform parent) where T : Component
12 {
13 T prefab = Resources.Load<T>(typeof (T).Name);
14 T control = Instantiate(prefab);
15 control.transform.SetParent(parent, false);
16 control.transform.localPosition = Vector3.zero;
17 control.transform.localRotation = Quaternion.identity;
18 control.transform.localScale = Vector3.one;
19 return control;
20 }
21
22 protected static MeshDraft Platform(float radius, float heignt, int segments = 128)
23 {
24 float segmentAngle = 360f/segments;
25 float currentAngle = 0;
26
27 var lowerRing = new List<Vector3>(segments);
28 var upperRing = new List<Vector3>(segments);
29 for (var i = 0; i < segments; i++)
30 {
31 var lowerPoint = PTUtils.PointOnCircle3XZ(radius + heignt, currentAngle);
32 lowerRing.Add(lowerPoint + Vector3.down*heignt);
33
34 var upperPoint = PTUtils.PointOnCircle3XZ(radius, currentAngle);
35 upperRing.Add(upperPoint);
36 currentAngle -= segmentAngle;
37 }
38
39 var platform = new MeshDraft {name = "Platform"};
40 var bottom = MeshDraft.TriangleFan(lowerRing);
41 bottom.Add(MeshDraft.Band(lowerRing, upperRing));
42 bottom.Paint(new Color(0.5f, 0.5f, 0.5f, 1));
43 platform.Add(bottom);
44
45 upperRing.Reverse();
46 var top = MeshDraft.TriangleFan(upperRing);
47 top.Paint(new Color(0.8f, 0.8f, 0.8f, 1));
48 platform.Add(top);
49
50 return platform;
51 }
52
53 protected static void AssignDraftToMeshFilter(MeshDraft draft, MeshFilter meshFilter, ref Mesh mesh)
54 {
55 if (mesh == null)
56 {
57 mesh = draft.ToMesh();
58 }
59 else
60 {
61 draft.ToMesh(ref mesh);
62 }
63 mesh.RecalculateBounds();
64 meshFilter.sharedMesh = mesh;
65 }
66
67 protected void GeneratePalette()
68 {
69 List<ColorHSV> palette = RandomE.TetradicPalette(0.25f, 0.7f);
70 targetPalette.mainColor = palette[0].WithSV(0.8f, 0.6f);
71 targetPalette.secondaryColor = palette[1].WithSV(0.8f, 0.6f);
72 targetPalette.skyColor = palette[2];
73 targetPalette.horizonColor = palette[3];
74 targetPalette.groundColor = ColorHSV.Lerp(targetPalette.skyColor, targetPalette.horizonColor, 0.5f);
75 }
76
77 protected ColorHSV GetMainColorHSV()
78 {
79 return targetPalette.mainColor;
80 }
81
82 protected ColorHSV GetSecondaryColorHSV()
83 {
84 return targetPalette.secondaryColor;
85 }
86
87 protected void SetupSkyboxAndPalette()
88 {
89 RenderSettings.skybox = new Material(RenderSettings.skybox);
90 currentPalette.mainColor = targetPalette.mainColor;
91 currentPalette.secondaryColor = targetPalette.secondaryColor;
92 currentPalette.skyColor = targetPalette.skyColor;
93 currentPalette.horizonColor = targetPalette.horizonColor;
94 currentPalette.groundColor = targetPalette.groundColor;
95 }
96
97 protected void UpdateSkybox()
98 {
99 LerpSkybox(RenderSettings.skybox, currentPalette, targetPalette, Time.deltaTime);
100 }
101
102 private static void LerpSkybox(Material skybox, Palette currentPalette, Palette targetPalette, float t)
103 {
104 currentPalette.skyColor = ColorHSV.Lerp(currentPalette.skyColor, targetPalette.skyColor, t);
105 currentPalette.horizonColor = ColorHSV.Lerp(currentPalette.horizonColor, targetPalette.horizonColor, t);
106 currentPalette.groundColor = ColorHSV.Lerp(currentPalette.groundColor, targetPalette.groundColor, t);
107
108 skybox.SetColor("_SkyColor", currentPalette.skyColor.ToColor());
109 skybox.SetColor("_HorizonColor", currentPalette.horizonColor.ToColor());
110 skybox.SetColor("_GroundColor", currentPalette.groundColor.ToColor());
111 }
112
113 private class Palette
114 {
115 public ColorHSV mainColor;
116 public ColorHSV secondaryColor;
117 public ColorHSV skyColor;
118 public ColorHSV horizonColor;
119 public ColorHSV groundColor;
120 }
121 }
122 }